using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;

using Entities;
using IDAL;

namespace SQLDBDAL
{
    public class CompanyDAO : ICompanyDAO
    {
        private const string SQL_SELECT_COMPANY = "SELECT * FROM Company";
        private const string SQL_UPDATE_COMPANY = "UPDATE Company SET CompanyName=@CompanyName, Description=@Description, Address=@Address, Phone=@Phone ," +
                                                   "Poster=@Poster WHERE CompanyID=@CompanyID";

        private const string SQL_UPDATE_COMPANY_POSTER = "UPDATE Company SET Poster=@Poster WHERE CompanyID=1";

        private const string PARAM_COMPANY_COMPANY_ID = "@CompanyID";
        private const string PARAM_COMPANY_COMPANY_NAME = "@CompanyName";
        private const string PARAM_COMPANY_DESCRIPTION = "@Description";
        private const string PARAM_COMPANY_ADDRESS = "@Address";
        private const string PARAM_COMPANY_PHONE = "@Phone";
        private const string PARAM_COMPANY_POSTER = "@Poster";

        #region ICompanyDAO Members

        public IList<CompanyInfo> GetAll()
        {
            IList<CompanyInfo> comIn = new List<CompanyInfo>();
            using (SqlDataReader reader = SQLDBHelper.ExecuteReader(SQLDBHelper.CONN_STRING,
                CommandType.Text, SQL_SELECT_COMPANY, null))
            {
                while (reader.Read())
                {
                    CompanyInfo comp = new CompanyInfo();
                    comp.CompanyID = reader.GetInt32(0);
                    comp.CompanyName = reader.GetString(1);
                    comp.Description = reader.GetString(2);
                    comp.Address = reader.GetString(3);
                    comp.Phone = reader.GetString(4);
                    if (!reader.IsDBNull(5))
                        comp.Poster = reader.GetString(5);
                    comIn.Add(comp);
                }
            }
            return comIn;
        }

        public bool Update(CompanyInfo comp)
        {
            SqlParameter[] parms;
            parms = new SqlParameter[] {     new SqlParameter(PARAM_COMPANY_COMPANY_NAME, SqlDbType.NVarChar),
                                             new SqlParameter(PARAM_COMPANY_DESCRIPTION, SqlDbType.NVarChar),
											 new SqlParameter(PARAM_COMPANY_ADDRESS, SqlDbType.NVarChar),
                                             new SqlParameter(PARAM_COMPANY_PHONE, SqlDbType.NVarChar),
                                             new SqlParameter(PARAM_COMPANY_POSTER, SqlDbType.NVarChar),
                                             new SqlParameter(PARAM_COMPANY_COMPANY_ID, SqlDbType.Int)};


            parms[0].Value = comp.CompanyName;
            parms[1].Value = comp.Description;
            parms[2].Value = comp.Address;
            parms[3].Value = comp.Phone; ;
            parms[4].Value = comp.Poster;
            parms[5].Value = comp.CompanyID;
           

            try
            {
                int result = 0;
                result = SQLDBHelper.ExecuteNonQuery(SQLDBHelper.CONN_STRING, CommandType.Text, SQL_UPDATE_COMPANY, parms);
                return (result > 0 ? true : false);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region bool UpdateImage(string imagePath)
        public bool UpdateImage(string imagePath)
        {
            SqlParameter[] parms = new SqlParameter[] { new SqlParameter(PARAM_COMPANY_POSTER, SqlDbType.NVarChar), new SqlParameter(PARAM_COMPANY_COMPANY_ID, SqlDbType.Int) };
            parms[0].Value = imagePath;
            parms[1].Value = 1;

            try
            {
                int result = SQLDBHelper.ExecuteNonQuery(SQLDBHelper.CONN_STRING, CommandType.Text, SQL_UPDATE_COMPANY_POSTER, parms);
                return (result > 0 ? true : false);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
        #endregion
    }
}
